var targetType; //所选授权维度类型
var targetId; 	//所选授权维度ID
var targetId;		//所选用户ID;群组授权时保存群组ID
var checkedRows = new Array();  //当前用户下用户权限被选中的行ID集合
var authServceId = "LANTOP_AUTH_SERVER_USERSET";	//服务ID
var authCount = 0;	//权限类型数量
var userInit = "";//false为新用户,true为旧用户
//var nodeIdArry = new Array();        //页面上权限发生变化的nodeId数组 (20160708 add)
var nodeIdsChecked = "";               //选中复选框(20160711 add 条目浏览)
var nodeIdsQwChecked = "";             //选中复选框(20160802 add 全文浏览)
var targetName;
$(function() {
	parent.childLoadSuccess = true;	//父页面属性，页面加载完成，更新父页面属性状态
	targetType = parent.TARGET_TYPE;
	targetId = parent.TARGET_ID;
	targetName = parent.TARGET_NAME;
	//userDatagridLoad();
	$('#userAuthTreegrid').treegrid({
		onContextMenu: function(e, row) {
			//树节点右击菜单
			e.preventDefault();
			var authString = row.OPERATE_AUTH;
			targetNodeId = row.ID;
			if (null != authString) {
				var authArray = authString.split(',');
				var tmll = authArray[0];
				var qwll = authArray[1];
				var qwdy = authArray[2];
				var qwxz = authArray[3];
				if (tmll == '1') {
					$('#addTmll').css('display', 'none');
					$('#delTmll').css('display', 'block');
				} else {
					$('#addTmll').css('display', 'block');
					$('#delTmll').css('display', 'none');
				}
				if (qwll == '1') {
					$('#addQwll').css('display', 'none');
					$('#delQwll').css('display', 'block');
				} else {
					$('#addQwll').css('display', 'block');
					$('#delQwll').css('display', 'none');
				}
				if (qwdy == '1') {
					$('#addQwdy').css('display', 'none');
					$('#delQwdy').css('display', 'block');
				} else {
					$('#addQwdy').css('display', 'block');
					$('#delQwdy').css('display', 'none');
				}
				if (qwxz == '1') {
					$('#addQwxz').css('display', 'none');
					$('#delQwxz').css('display', 'block');
				} else {
					$('#addQwxz').css('display', 'block');
					$('#delQwxz').css('display', 'none');
				}
			} else {
				$('#addTmll').css('display', 'block');
				$('#addQwll').css('display', 'block');
				$('#addQwdy').css('display', 'block');
				$('#addQwxz').css('display', 'block');
				$('#delTmll').css('display', 'none');
				$('#delQwll').css('display', 'none');
				$('#delQwdy').css('display', 'none');
				$('#delQwxz').css('display', 'none');
			}
			$(this).treegrid('select', row.ID);
			$('#treeMenu').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
	});
});

var targetNodeId = "";
//设置权限
function saveAuth(authString) {
	parent.maskDo();
	$.ajax({
		url : 'platform/UserAuthSetController/saveUserSetDataAuthBySelect',
		data : {
			targetType : targetType,
			targetId : targetId,
			authString: authString,
			targetNodeId: targetNodeId
			},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			parent.unMaskDo();
			if (result.flag == "0") {
				operateRoleSetAfter();	//保存成功刷新用户权限设置信息
				
				$.messager.show({
					title : '提示',
					msg : "档案用户权限设置成功！"
				});
			}else{
				$.messager.show({
					title : '提示',
					msg : "档案用户权限设置失败！"
				});
			}
		}
	});
}

/**
 * 加载所选授权维度下用户
 */
function userDatagridLoad(){
	//授权时，userId保存各维度ID
	userId = targetId;
	$("#userAuthTreegrid").datagrid("clearChecked");
	//授权显示用户权限设置信息
	$("#userAuthTreegrid").treegrid("options").url = "platform/UserAuthSetController/getArchiveClass";
	$('#userAuthTreegrid').treegrid('load', {
		targetType : targetType,
		targetId : targetId
	});
	
	$("#userDatagrid").datagrid("options").url = "platform/UserAuthSetController/getAuthUser";
	$('#userDatagrid').datagrid('load', {
		targetType : targetType,
		targetId : targetId,
		targetName:targetName
	});
}

/**
 * 选中用户事件
 * @param rowIndex
 * @param rowData
 */
function userOnClickRow(){
	if(targetType == "3"){//是用户分组时执行
		$("#userAuthTreegrid").datagrid("clearChecked");
		//用户授权，重新加载用户则重新加载用户权限设置
		$("#userAuthTreegrid").treegrid("options").url = "platform/UserAuthSetController/getArchiveClass";
		$('#userAuthTreegrid').treegrid('load', {
			targetType : targetType,
			targetId : targetId
		});
	}
}

/**
 * 用户选择datagrid数据加载完成事件：当没有选中用户时，右侧用不权限设置treegrid也同时清空
 * @param data
 */
function userOnLoadSuccess(data){
	if(targetType != "3"){
		userOnClickRow();
		return;
	}
	$("#userDatagrid").datagrid("selectRow", 0);
	var rowData = $("#userDatagrid").datagrid("getSelected");
	if(rowData != null){
		userOnClickRow();
		return;
	}
	//用户授权，重新加载用户则重新加载用户权限设置
	$("#userAuthTreegrid").datagrid("clearChecked");
	$("#userAuthTreegrid").treegrid("options").url = "platform/UserAuthSetController/getArchiveClass";
	$('#userAuthTreegrid').treegrid('load', {
		targetId : ''
	});
}

/**
 * 将权限咧转化为复选框
 */
function formatCheckbox(value, rowData, rowIndex) {
	var c = "";
	var colName = value.substring(0,2);		//数据中前两位用来保存列名
	if(value == (colName+"1") || userInit == "false"){
		checkedRows.push(rowData.ID);	//记录下所有授权类型被选中的行ID
		c = "<input type='checkbox' value='"+colName+"1' id='"+rowData.ID+colName+"' checked='checked' disabled='disabled'  />";//onclick='onCheckboxClick(\""+rowData.ID+colName+"\",\""+rowData.ID+"\",\""+colName+"\")'
	} else{
	    c = "<input type='checkbox' value='"+colName+"0' id='"+rowData.ID+colName+"' disabled='disabled' />";//onclick='onCheckboxClick(\""+rowData.ID+colName+"\",\""+rowData.ID+"\",\""+colName+"\")'
	}
	return c;
}

/**
 * 复选框点击事件：点击复选框后更新对应的行记录
 * @param checkboxId
 * @param rowId
 * @param colName
 */
function onCheckboxClick(checkboxId,rowId,colName){
	var checkbox = document.getElementById(checkboxId);
	var index = colName.substring(1);	//获取复选框顺序索引
	var name = colName.substring(0,1);
	if(checkbox.checked){
		//勾选当前复选框之前的所有复选框
		var rowStr = '{'; 
		for(var i = index; i > 0; i--){
			var val = name + i + "1";
			rowStr += '"' + name + i +'": "'+val+'",';
		}
		rowStr = rowStr.substring(0, rowStr.length-1) + "}";
		var row = JSON.parse(rowStr);
		$('#userAuthTreegrid').treegrid('update',{
			id: rowId,
			row: row
		});
		
		//20160711
		if(rowStr.indexOf("A11")>0){  //条目浏览
			if(nodeIdsChecked==""){
				nodeIdsChecked = nodeIdsChecked +rowId;
			}else{
				nodeIdsChecked = nodeIdsChecked + "," + rowId;
			}
		}
		//20160802
		if(rowStr.indexOf("A21")>0){  //全文浏览
			if(nodeIdsQwChecked==""){
				nodeIdsQwChecked = nodeIdsQwChecked +rowId;
			}else{
				nodeIdsQwChecked = nodeIdsQwChecked + "," + rowId;
			}
		}
		//
		var childrenRows = $('#userAuthTreegrid').treegrid('getChildren', rowId);
		for(var i = 0; i < childrenRows.length; i++){
			$("#userAuthTreegrid").treegrid("select", childrenRows[i].ID); 	//被操作的数据节点选中，用于提交数据
			checkedRows.push(childrenRows[i].ID);	//记录下所有授权类型被选中的行ID
			//勾选当前复选框之前的所有复选框
			var childrenRowStr = '{'; 
			for(var j = index; j > 0; j--){
				var val = name + j + "1";
				childrenRowStr += '"' + name + j +'": "'+val+'",';
			}
			childrenRowStr = childrenRowStr.substring(0, childrenRowStr.length-1) + "}";
			var childrenRow = JSON.parse(childrenRowStr);
			$('#userAuthTreegrid').treegrid('update',{
				id: childrenRows[i].ID,
				row: childrenRow
			});
		}
	}else{
		//取消勾选当前复选框之后的所有复选框
		var rowStr = '{'; 
		for(var i = index; i <= authCount; i++){
			var val = name + i + "0";
			rowStr += '"' + name + i +'": "'+val+'",';
		}
		rowStr = rowStr.substring(0, rowStr.length-1) + "}";
		var row = JSON.parse(rowStr);
		$('#userAuthTreegrid').treegrid('update',{
			id: rowId,
			row: row
		});
		//20160711-条目浏览
		if(rowStr.indexOf("A10")>0){  //去掉选中
			if(nodeIdsChecked==""){
				nodeIdsChecked = nodeIdsChecked + rowId;
			}else{
				nodeIdsChecked = nodeIdsChecked + "," + rowId;
			}
		}
		//20160802-全文浏览
		if(rowStr.indexOf("A20")>0){  //去掉选中
			if(nodeIdsQwChecked==""){
				nodeIdsQwChecked = nodeIdsQwChecked + rowId;
			}else{
				nodeIdsQwChecked = nodeIdsQwChecked + "," + rowId;
			}
		}
		var childrenRows = $('#userAuthTreegrid').treegrid('getChildren', rowId);
		for(var i = 0; i < childrenRows.length; i++){
			//勾选当前复选框之后的所有复选框
			var childrenRowStr = '{'; 
			for(var j = index; j <= authCount; j++){
				var val = name + j + "0";
				childrenRowStr += '"' + name + j +'": "'+val+'",';
			}
			childrenRowStr = childrenRowStr.substring(0, childrenRowStr.length-1) + "}";
			var childrenRow = JSON.parse(childrenRowStr);
			$('#userAuthTreegrid').treegrid('update',{
				id: childrenRows[i].ID,
				row: childrenRow
			});
		}
	}
	$("#userAuthTreegrid").treegrid("select", rowId); 	//被操作的数据节点选中，用于提交数据
}

/**
 * 保存
 */
function saveUserSetDataAuth(){
	parent.maskDo();
	var nodeIdSel=nodeIdsChecked;       //20160708 add(条目浏览)
	var nodeIdQwSel=nodeIdsQwChecked;   //20160802 add(全文浏览)
	var allRows = $("#userAuthTreegrid").treegrid("getSelections");
	var datas = JSON.stringify(allRows);
	$.ajax({
		url : 'platform/UserAuthSetController/saveUserSetDataAuth',
		data : {
			datas : datas,
			targetType : targetType,
			targetId : targetId,
			nodeIdCheck :nodeIdSel,
			nodeIdQwCheck :nodeIdQwSel
			},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			parent.unMaskDo();
			if (result.flag == "0") {
				operateRoleSetAfter();	//保存成功刷新用户权限设置信息
				
				$.messager.show({
					title : '提示',
					msg : "档案用户权限设置成功！"
				});
			}else{
				$.messager.show({
					title : '提示',
					msg : "档案用户权限设置失败！"
				});
			}
		}
	});
}

/**
 * 初始化用户权限
 */
function saveInitUserSetDataAuth(){
	userInit = "true";
	var allRows = $("#userAuthTreegrid").treegrid("getSelections");
	var datas = JSON.stringify(allRows);
	$.ajax({
		url : 'platform/UserAuthSetController/saveInitUserSetDataAuth',
		data : {
			datas : datas,
			targetType : targetType,
			targetId : targetId
			},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			if (result.flag == "0") {
				userInit = result.userInit;	//更新初始化标识状态
				operateRoleSetAfter();	//保存成功刷新用户权限设置信息
			}
		}
	});
}

/**
 * 用户权限设置treegrid取消选中事件：保存时提交所有选中记录，所以数据节点被操作后不能取消选中
 * @param rowData
 */
function userAuthUnSelect(rowData){
	$("#userAuthTreegrid").treegrid("select", rowData.ID);
}

/**
 * 用户权限设置treegrid数据加载完成事件：默认勾选存在权限设置的行
 * @param row
 * @param data
 */
function userAuthOnLoadsuccess(row, data){
	for(var i = 0; i < checkedRows.length; i++){
		$("#userAuthTreegrid").treegrid("select", checkedRows[i]); 	//被操作的数据节点选中，用于提交数据
	}
	checkedRows = new Array();
	
	if(userInit == "false"){
		//保存初始化权限
		saveInitUserSetDataAuth();
	}
}

/**
 * 操作角色设置超链接
 */
function formatOperateRoleSet(value, rowData, rowIndex) {
	if(rowData.ARCHIVE_CLASS_NODE_TYPE == 2){	//节点为数据节点
		if(value == null || value == ""){
			return "<a href=\"javascript:operateRoleSet('"
					+ rowData.ARCHIVE_CLASS_NODE_NAME + "','"
					+ rowData.REL_TARGET_ID + "','"
					+ rowData.REL_TARGET_NAME + "','"
					+ rowData.CBB_AUTH_RESOURCE_ID + "','"
					+ rowData.TARGET_TYPE + "','" + targetId
					+ "','LANTOP_AUTH_SERVER_USERSET','" + rowData.RES_CODE
					+ "','" + rowData.RES_NAME + "','" + rowData.ACTION_TYPE
					+ "','" + rowData.OPERATE_AUTH + "')\">"+ commonLang.optip.content19 +"</a>";
		}
		else{
			return "<a href=\"javascript:operateRoleSet('"
					+ rowData.ARCHIVE_CLASS_NODE_NAME + "','"
					+ rowData.REL_TARGET_ID + "','"
					+ rowData.REL_TARGET_NAME + "','"
					+ rowData.CBB_AUTH_RESOURCE_ID + "','"
					+ rowData.TARGET_TYPE + "','" + targetId
					+ "','LANTOP_AUTH_SERVER_USERSET','" + rowData.RES_CODE
					+ "','" + rowData.RES_NAME + "','" + rowData.ACTION_TYPE
					+ "','" + rowData.OPERATE_AUTH + "')\">" + value + "</a>";
		}
	}
}

/**
 * 打开操作权限设置页面
 * @param rowId
 */
function operateRoleSet(archiveClassNodeName,relTargetId,relTargetNames,cbbAuthResourceId,targetType,targetId,authServiceId,resCode,resName,actionType,operateAuth){
	parent.operateRoleSet_open(archiveClassNodeName,relTargetId,relTargetNames,cbbAuthResourceId,targetType,targetId,authServiceId,resCode,resName,actionType,operateAuth);
	
}

/**
 * 操作角色设置窗口关闭后调用方法，刷新操作角色列表
 */
function operateRoleSetAfter(dialogId){
	if(dialogId != null && dialogId != ""){
		$('#' + dialogId).dialog('close');
	}
	$("#userAuthTreegrid").datagrid("clearChecked");
	$("#userAuthTreegrid").treegrid("options").url = "platform/UserAuthSetController/getArchiveClass";
	$('#userAuthTreegrid').treegrid('load', {
		targetType : targetType,
		targetId : targetId
	});
}

/**
 * 条目权限设置
 */
function entryAuthSet(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.entryAuthSet_open(checkedRows,targetType,targetId,authServceId);
}

/**
 * 新增条件
 */
function addQualification(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.addQualification_open(checkedRows,targetType,targetId,authServceId);
}

/**
 * 编辑条件
 */
function editQualification(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.editQualification_open(checkedRows,targetType,targetId,authServceId);
}

/**
 * 删除条件
 */
function delQualification(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.delQualification_open(checkedRows,targetType,targetId,authServceId);
}


/**
 * 批量操作权限设置
 */
function batchOperateAuthSet(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.batchOperateAuthSet_open_new(checkedRows,targetType,targetId,authServceId);
}

/**
 * 用户权限设置treegrid父节点勾选后自动勾选所有子节点
 * @param rowData
 */
function userAuthOnCheck(rowData){
	var childrenRows = $("#userAuthTreegrid").treegrid("getChildren",rowData.ID);
	for(var i = 0; i < childrenRows.length; i++){
		$("#userAuthTreegrid").treegrid('checkRow',childrenRows[i].ID);
	}
}

/**
 * 用户权限设置treegrid父节点取消勾选后自动取消勾选所有子节点
 * @param rowData
 */
function userAuthOnUnCheck(rowData){
	var childrenRows = $("#userAuthTreegrid").treegrid("getChildren",rowData.ID);
	for(var i = 0; i < childrenRows.length; i++){
		$("#userAuthTreegrid").treegrid('uncheckRow',childrenRows[i].ID);
	}
}

/**
 * 删除权限
 */
function delAuthSet(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	if(checkedRows.length < 1){
		//commonLang.tip.alert替换'提示',commonLang.optip.del.selectNode替换'请选择数据节点！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.del.selectNode, 'warning');
	}else{
		var ids = []; 
		for(var i = 0; i < checkedRows.length; i++){
			ids.push(checkedRows[i].ID);
		}
		//commonLang.tip.confirm替换'请确认',commonLang.optip.del.delAuthSet替换'您确定要删除用户权限设置？'
		$.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.delAuthSet, function(b) {
			if(!b){
				return;
			}
			$.ajax({
				url : 'platform/UserAuthSetController/delAuthSet',
				data : {
					codes : ids.join(','),
					targetType : targetType,
					targetId : targetId
					},
				type : 'post',
				dataType : 'json',
				success : function(result) {
					if (result.flag == "0") {
						$("#userAuthTreegrid").datagrid("clearChecked");
						$("#userAuthTreegrid").treegrid("options").url = "platform/UserAuthSetController/getArchiveClass";
						$('#userAuthTreegrid').treegrid('load', {
							targetType : targetType,
							targetId : targetId
						});
						
						$.messager.show({
							title : '提示',
							//commonLang.optip.del.delAuthSuccess替换"删除权限成功！"
							msg : commonLang.optip.del.delAuthSuccess
						});
					}
					else{
						$.messager.show({
							title : '提示',
							//commonLang.optip.del.delAuthFail替换"删除权限失败！"
							msg : commonLang.optip.del.delAuthFail
						});
					}
				}
			});
		});
	}
}

/**
 * 条件复制
 */
function copyQualification(){
	var checkedRows = $("#userAuthTreegrid").datagrid("getChecked");
	parent.copyQualification_open(checkedRows,targetType,targetId);
}

/**
 * 权限复制
 */
function copyAuth(){
	var userRow = $("#userDatagrid").datagrid("getSelected");
	parent.copyAuth_open(userRow,targetType,targetId);
}

/**
 * add by heps
 * add date 2018-12-14
 * 编辑条件超链接 
 */
function formatEditCondition(value, rowData, rowIndex) {
	if(rowData.ARCHIVE_CLASS_NODE_TYPE == 2){	//节点为数据节点
		if("0" != value){
			return "<a href='javascript:parent.openEditQualification(\""+rowData.ARCHIVE_CLASS_NODE_TYPE+"\",\""+ rowData.ID+"\",\""+ rowData.ARCHIVE_CLASS_NODE_NAME + "\",\""+ targetType + "\",\"" + targetId +"\",\"" + authServceId + "\");'>编辑</a>";
		}
	}
}


